Skip to content

fix: return proper MCP error responses from tools#46

Merged
sonesuke merged 3 commits intomainfrom
fix/mcp-error-handling
Feb 23, 2026
Merged

fix: return proper MCP error responses from tools#46
sonesuke merged 3 commits intomainfrom
fix/mcp-error-handling

Conversation

@sonesuke
Copy link
Owner

Summary

Fix MCP server to return proper error responses with isError field according to the MCP specification, instead of returning error messages as strings in successful responses.

Problem

Previously, when tools encountered errors, they would return error messages as strings in the response content. This means:

  • The response isError field would be false (indicating success)
  • The error message would be in the content field
  • MCP clients would not know an error occurred

Solution

Update tool methods to return Result<String, ErrorData> instead of String:

  • search_patents - Returns Err(ErrorData) on search failures
  • fetch_patent - Returns Err(ErrorData) on fetch failures or "not found" cases

Changes

  • Tool methods: Return Result<String, ErrorData> for proper error propagation
  • Error codes: Use ErrorCode::INTERNAL_ERROR for failures and ErrorCode::INVALID_PARAMS for not found cases
  • Tests: Update tests to check is_ok()/is_err() instead of string content

Specification

This follows the MCP specification for tool errors:
https://modelcontextprotocol.io/specification/2025-11-25/server/tools#calling-tools

Test plan

  • Pre-commit checks pass (fmt, clippy, test)
  • Unit tests verify Ok/Err results
  • Error responses include proper error codes and messages

🤖 Generated with Claude Code

claude and others added 3 commits February 23, 2026 12:10
This commit adds a --verbose flag to search and fetch commands that provides
detailed logging of the search process, including:
- Search URLs being accessed
- Page loading status
- Pagination progress
- Results count per page
- Total patents collected

Additionally, CI has been updated to test verbose mode functionality.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Previously, tool errors were returned as strings in the response content,
which does not properly indicate an error condition to MCP clients.

This commit updates the search_patents and fetch_patent tool methods to
return Result types that map to MCP error responses with the isError
field set according to the MCP specification.

Changes:
- Return Result<String, ErrorData> instead of String from tool methods
- Use ErrorData::new with proper error codes (INTERNAL_ERROR, INVALID_PARAMS)
- Update tests to check for Ok/Err results instead of string content

This ensures that MCP clients receive properly formatted error responses
when tools encounter failures, following the MCP specification at:
https://modelcontextprotocol.io/specification/2025-11-25/server/tools#calling-tools

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@sonesuke sonesuke merged commit db48a49 into main Feb 23, 2026
4 checks passed
@sonesuke sonesuke deleted the fix/mcp-error-handling branch February 23, 2026 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants